<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Essay extends Controller
{
    public function showessay(){
        $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("essay_status",1)->order('a.essay_weight desc,essay_id desc')->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function byclassIdGetessay(){
        $essay_class_id=input("essay_class_id");
        $data=Db::name("essay")->where("essay_class_id",$essay_class_id)->where("essay_status",1)->order('qkl_essay.essay_weight desc,essay_id desc')->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function byclassIdGetTotalessay(){
        $countStart=input("countStart");
        $countEnd=input("countEnd");
        $essay_class_id=input("essay_class_id");
        $isbehavior=empty(input('isbehavior'))?0:input('isbehavior');
        if(empty($countEnd))
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name',essay_add_date AS '添加时间|essay_add_date'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->order('a.essay_weight desc,essay_id desc')->select(); 
        else
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name',essay_add_date AS '添加时间|essay_add_date'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->order('a.essay_weight desc,essay_id desc')->limit($countStart,$countEnd)->select();
        $essay_attribute=Db::name("essay_model")->field("essay_attribute_name,c.essay_attribute_id,essay_attribute_code")->alias("a")->join("essay_attribute c","a.essay_attribute_id=c.essay_attribute_id")->where("essay_class_id",$essay_class_id)->select(); 
        for($i=0;$i<count($data);$i++){
            for($j=0;$j<count($essay_attribute);$j++){
                $data[$i][$essay_attribute[$j]['essay_attribute_name'].'|'.$essay_attribute[$j]['essay_attribute_code']]=Db::name("essay_attribute_value")->where("essay_attribute_id",$essay_attribute[$j]['essay_attribute_id'])->where("essay_id",$data[$i]['essay_id'])->value("essay_attribute_value");
            }
            if($isbehavior==1){
                $user_behavor_sum=Db::query("SELECT user_behavior_type,COUNT(*) AS countsum FROM qkl_user_behavior where essay_id={$data[$i]['essay_id']} GROUP BY user_behavior_type");
                for($z=0;$z<count($user_behavor_sum);$z++)
                    $data[$i][$user_behavor_sum[$z]['user_behavior_type']]=$user_behavor_sum[$z]['countsum'];
            }
        }
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function byclassIdGetTotalessayLikeTitle(){
        $countStart=input("countStart");
        $countEnd=input("countEnd");
        $essay_title=input("essay_title");
        $essay_class_id=input("essay_class_id");
        $isbehavior=empty(input('isbehavior'))?0:input('isbehavior');
        if(empty($countEnd))
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->where("essay_title like '%{$essay_title}%'")->order('a.essay_weight desc,essay_id desc')->select(); 
        else
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->where("essay_title like '%{$essay_title}%'")->order('a.essay_weight desc,essay_id desc')->limit($countStart,$countEnd)->select();
        $essay_attribute=Db::name("essay_model")->field("essay_attribute_name,c.essay_attribute_id,essay_attribute_code")->alias("a")->join("essay_attribute c","a.essay_attribute_id=c.essay_attribute_id")->where("essay_class_id",$essay_class_id)->select(); 
        for($i=0;$i<count($data);$i++){
            for($j=0;$j<count($essay_attribute);$j++){
                $data[$i][$essay_attribute[$j]['essay_attribute_name'].'|'.$essay_attribute[$j]['essay_attribute_code']]=Db::name("essay_attribute_value")->where("essay_attribute_id",$essay_attribute[$j]['essay_attribute_id'])->where("essay_id",$data[$i]['essay_id'])->value("essay_attribute_value");
            }
            if($isbehavior==1){
                $user_behavor_sum=Db::query("SELECT user_behavior_type,COUNT(*) AS countsum FROM qkl_user_behavior where essay_id={$data[$i]['essay_id']} GROUP BY user_behavior_type");
                for($z=0;$z<count($user_behavor_sum);$z++)
                    $data[$i][$user_behavor_sum[$z]['user_behavior_type']]=$user_behavor_sum[$z]['countsum'];
            }
        }
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function byclassIdGetTotalessayLikeTime(){
        $countStart=input("countStart");
        $countEnd=input("countEnd");
        $essay_releasetime=input("essay_releasetime");
        $essay_class_id=input("essay_class_id");
        $isbehavior=empty(input('isbehavior'))?0:input('isbehavior');
        if(empty($countEnd))
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->where("essay_releasetime",$essay_releasetime)->order('a.essay_weight desc,essay_id desc')->select(); 
        else
            $data=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->where("essay_status",1)->where("essay_releasetime",$essay_releasetime)->order('a.essay_weight desc,essay_id desc')->limit($countStart,$countEnd)->select();
        $essay_attribute=Db::name("essay_model")->field("essay_attribute_name,c.essay_attribute_id,essay_attribute_code")->alias("a")->join("essay_attribute c","a.essay_attribute_id=c.essay_attribute_id")->where("essay_class_id",$essay_class_id)->select(); 
        for($i=0;$i<count($data);$i++){
            for($j=0;$j<count($essay_attribute);$j++){
                $data[$i][$essay_attribute[$j]['essay_attribute_name'].'|'.$essay_attribute[$j]['essay_attribute_code']]=Db::name("essay_attribute_value")->where("essay_attribute_id",$essay_attribute[$j]['essay_attribute_id'])->where("essay_id",$data[$i]['essay_id'])->value("essay_attribute_value");
            }
            if($isbehavior==1){
                $user_behavor_sum=Db::query("SELECT user_behavior_type,COUNT(*) AS countsum FROM qkl_user_behavior where essay_id={$data[$i]['essay_id']} GROUP BY user_behavior_type");
                for($z=0;$z<count($user_behavor_sum);$z++)
                    $data[$i][$user_behavor_sum[$z]['user_behavior_type']]=$user_behavor_sum[$z]['countsum'];
            }
        }
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function byIdGetTotelessay(){
        $essay_id=input("essay_id");
        $isbehavior=empty(input('isbehavior'))?0:input('isbehavior');
        $data=[];
        $essay=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("essay_id",$essay_id)->find();
        if($isbehavior==1){
            $user_behavor_sum=Db::query("SELECT user_behavior_type,COUNT(*) AS countsum FROM qkl_user_behavior where essay_id={$essay_id} GROUP BY user_behavior_type");
            for($z=0;$z<count($user_behavor_sum);$z++)
                $essay[$user_behavor_sum[$z]['user_behavior_type']]=$user_behavor_sum[$z]['countsum'];
        }
        $essay_attribute_name=Db::name("essay_model")->field("essay_attribute_name,c.essay_attribute_id,essay_attribute_code")->alias("a")->join("essay_attribute c","a.essay_attribute_id=c.essay_attribute_id")->where("essay_class_id",$essay['文章分类ID|essay_class_id'])->select();
        for($i=0;$i<count($essay_attribute_name);$i++)
            $data[$essay_attribute_name[$i]["essay_attribute_name"].'|'.$essay_attribute_name[$i]["essay_attribute_code"]]=Db::name("essay_attribute_value")->where("essay_attribute_id",$essay_attribute_name[$i]['essay_attribute_id'])->where("essay_id",$essay_id)->value("essay_attribute_value");
        return ['status'=>1,'message'=>'获取成功','essay'=>$essay,'essay_extra'=>$data];
    }

    public function byclassIdGetTotalOneEssay(){
        $essay_class_id=input('essay_class_id');
        $data=[];
        $essay=Db::name("essay")->alias('a')->field("essay_id,essay_title AS '文章标题|essay_title',essay_content_textarea AS '文章内容|essay_content_textarea',essay_releasetime AS '发布时间|essay_releasetime',essay_pageview AS '浏览量|essay_pageview',essay_releasepeople AS '发布人|essay_releasepeople',essay_shortcontent AS '短内容|essay_shortcontent',a.essay_class_id AS '文章分类ID|essay_class_id',a.essay_weight AS '权重|essay_weight',essay_status AS '状态|essay_status',essay_image_file AS '文章封面链接|essay_image_file',essay_short_title AS '短标题|essay_short_title',essay_name AS '分类名|essay_name'")->join("essay_class c","a.essay_class_id=c.essay_class_id")->where("a.essay_class_id",$essay_class_id)->find();
        $essay_attribute_name=Db::name("essay_model")->field("essay_attribute_name,c.essay_attribute_id,essay_attribute_code")->alias("a")->join("essay_attribute c","a.essay_attribute_id=c.essay_attribute_id")->where("essay_class_id",$essay['文章分类ID|essay_class_id'])->select();
        for($i=0;$i<count($essay_attribute_name);$i++)
            $data[$essay_attribute_name[$i]["essay_attribute_name"].'|'.$essay_attribute_name[$i]["essay_attribute_code"]]=Db::name("essay_attribute_value")->where("essay_attribute_id",$essay_attribute_name[$i]['essay_attribute_id'])->where("essay_id",$essay['essay_id'])->value("essay_attribute_value");
        return ['status'=>1,'message'=>'获取成功','essay'=>$essay,'essay_extra'=>$data];
    }

    public function updatebyonecolumn(){
        $id=input('id');
        $column=input('column');
        $value=input('value');
        if(Db::name('essay')->where('essay_id',$id)->setField($column,$value))
            return ['status'=>1,'message'=>'操作成功'];
        else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function addessay(){
        $data=input();
        if(Db::name("essay")->insert($data)){
            $essay_id=Db::name("essay")->getLastInsID();
            return ['status'=>1,'message'=>'添加成功','essay_id'=>$essay_id];
        }
        else
            return ['status'=>0,'message'=>'添加失败'];
    }

    public function addextraessay(){
        $data=input();
        $essay_id=$data['essay_id'];
        unset($data['essay_id']);
        Db::startTrans();
        try{
            foreach($data as $column=>$value){
                $essay_attribute_id=Db::name("essay_attribute")->where("essay_attribute_code",$column)->value("essay_attribute_id");
                Db::name("essay_attribute_value")->data(['essay_id'=>$essay_id,'essay_attribute_id'=>$essay_attribute_id,'essay_attribute_value'=>$value])->insert();   
            }
            // 提交事务
            Db::commit();    
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['status'=>0,'message'=>'添加失败'];  
        }

        return ['status'=>1,'message'=>'添加成功'];
    }

    public function deleteessay(){
        $essay_id=input("essay_id");
        if(Db::name("essay")->where("essay_id",$essay_id)->setField("essay_status",0))
            return ['status'=>1,'message'=>'操作成功'];
        else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function updateessay(){
        $data=input();
        $essay_id=$data['essay_id'];
        $data1=Db::name('essay')->where('essay_id',$essay_id)->find();
        if(companyarray($data,$data1)){
            return ['status'=>1,'message'=>'操作成功','essay_id'=>$essay_id];
        }
        unset($data['essay_id']);
        if(Db::name("essay")->where("essay_id",$essay_id)->update($data))
            return ['status'=>1,'message'=>'操作成功','essay_id'=>$essay_id];
        else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function updateextraessay(){
        $data=input();
        $essay_id=$data['essay_id'];
        unset($data['essay_id']);
        Db::startTrans();
        try{
            foreach($data as $column=>$value){
                    $essay_attribute_id=Db::name("essay_attribute")->where("essay_attribute_code",$column)->value("essay_attribute_id");
                    if(Db::name("essay_attribute_value")->where(['essay_attribute_id'=>$essay_attribute_id,'essay_id'=>$essay_id])->count()>=1)
                        Db::name("essay_attribute_value")->where(['essay_attribute_id'=>$essay_attribute_id,'essay_id'=>$essay_id])->setField("essay_attribute_value",$value); 
                    else
                        Db::name("essay_attribute_value")->insert(['essay_attribute_id'=>$essay_attribute_id,'essay_id'=>$essay_id,'essay_attribute_value'=>$value]);
                }
            Db::commit();   
        }catch(\Exception $e){
            Db::rollback();
            return ['status'=>0,'message'=>'操作失败','data'=>$e->getMessage()];  
        }
        return ['status'=>1,'message'=>'操作成功'];
    }
}